package my.jbf.firstGame.BITMAP;


import static java.lang.Thread.sleep;

import android.graphics.*;
import android.view.*;
import my.jbf.firstGame.GAMEDATA.*;
import my.jbf.firstGame.*;
import my.jbf.firstGame.libraries.*;




public class AttackButton implements Runnable
{
	private boolean isNeedCommit=false;
	private DirectionInterface DInterface;
	private boolean isAttackButtonLive=true;

	@Override
	public void run()
	{
		// TODO: Implement this method
		long startTime=System.currentTimeMillis();
		while (isAttackButtonLive)
		{
			try {
				if(System.currentTimeMillis()-startTime<1000/240) {
					sleep(1000/240-(System.currentTimeMillis() - startTime));
				}
				startTime=System.currentTimeMillis();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			if (isNeedCommit)
			{
				DInterface.callback(getMoveContral());
				reDraw();
				isNeedCommit = false;
			}
		}
	}

	public void setAttackButtonTouchListener(MotionEvent event, DirectionInterface dinterface)
	{
		/**
		 *@Author JBF
		 *@Time 2021/11/17 上午8:41
		 *@Description 设置方向键触控监听,其实就是对传进来的MotionEvent做处理
		 **/
		this.DInterface = dinterface;
		switch (event.getActionMasked())
		{

			case MotionEvent.ACTION_DOWN:
			{
//					if (isInAttackButton(event.getX() - msp.getAttackButtonXY.x, event.getY() - msp.getAttackButtonXY.y))
//					{
//						isTouchDown = true;
//						setLeftButtonDown(event.getX() - msp.getAttackButtonXY.x, event.getY() - msp.getAttackButtonXY.y);
//						mTouchID = event.getPointerId(event.getActionIndex());
//					}
//					break;
			}
			case MotionEvent.ACTION_POINTER_DOWN:
			{
				if (isInAttackButton(event.getX(event.getActionIndex()) - LeftTopPoint.x, event.getY(event.getActionIndex()) - LeftTopPoint.y))
				{
					isTouchDown = true;
					setLeftButtonDown(event.getX(event.getActionIndex()) - LeftTopPoint.x, event.getY(event.getActionIndex()) - LeftTopPoint.y);
					mTouchID = event.getPointerId(event.getActionIndex());
				}
				break;
			}

			case MotionEvent.ACTION_MOVE:
			{
				if (isTouchDown)
				{
					setLeftButtonDown(event.getX(event.findPointerIndex(mTouchID)) - LeftTopPoint.x, event.getY(event.findPointerIndex(mTouchID)) - LeftTopPoint.y);
					isNeedCommit = true;
				}
				break;
			}
			case MotionEvent.ACTION_POINTER_UP:
			{
				if (event.getPointerId(event.getActionIndex()) == mTouchID)
				{
					mTouchID = -1;
					mTouchDownPoint.set(centerPointInside.x, centerPointInside.y);
					isTouchDown = false;
					isNeedCommit = true;
				}
				break;
			}
			case MotionEvent.ACTION_UP:
			{
				mTouchID = -1;
				mTouchDownPoint.set(centerPointInside.x, centerPointInside.y);
				isTouchDown = false;
				isNeedCommit = true;
				break;
			}
			case MotionEvent.ACTION_CANCEL:
			{
				isNeedCommit = true;
			}
		}
	}
	private  Canvas mCanvas;
	private  SharedPreferenceManager mSPM;
	private  Paint mPaint;
	private  Bitmap mBitmap;
	private  int OutCircleSize;
	private  PointF centerPointInside;//内部中点

	private  PointF LeftTopPoint;//外部左上角点，即图片放置点

	public boolean setisAttackButtonLive()
	{
		/*****************************
		 *@Author JBF
		 *@Time 2021/11/17 上午8:43
		 *@Description 让外部设
		 *****************************/
		isAttackButtonLive = !isAttackButtonLive;
		return isAttackButtonLive;
	}
	public AttackButton()
	{
		mSPM = MainActivity.mSPM;
		mMoveContral = new PointF(0f, 0f);
		LeftTopPoint = new PointF(mSPM.getAttackButtonXY().x - mSPM.getAttackButtonSize() / 2, mSPM.getAttackButtonXY().y - mSPM.getAttackButtonSize() / 2);//外部左上角
		centerPointInside = new PointF(mSPM.getAttackButtonSize() / 2, mSPM.getAttackButtonSize() / 2);//内部中心点
		mTouchDownPoint = new PointF();
		mTouchDownPoint.set(centerPointInside.x, centerPointInside.y);
		mPaint = new Paint();
		mPaint.setColor(mSPM.getAttackButtonColor());
		mPaint.setStyle(Paint.Style.STROKE);
		mBitmap = Bitmap.createBitmap(mSPM.getAttackButtonSize(), mSPM.getAttackButtonSize(), Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		OutCircleSize = mSPM.getAttackButtonSize() * 2 / 3;
		reDraw();
	}

	/*左侧按钮设置*/
	private boolean isTouchDown=false;
	private PointF mTouchDownPoint;
	private int mTouchID=-1;
	private  PointF mMoveContral;//给定外部数据用……不知道用什么名字比较合适

	/*左侧设置完毕*/










	private boolean isInAttackButton(float InSideX, float InSideY)
	{
		/*****************************
		 *@Author JBF
		 *@Time 2021/11/17 下午2:07
		 *@Description 判断外部点击是否在本控件内
		 *****************************/

		return InSideX >= 0 && InSideY >= 0 && InSideX <= mSPM.getAttackButtonSize() && InSideY <= mSPM.getAttackButtonSize();
	}


	public PointF getMoveContral()
	{
		float x=(mTouchDownPoint.x - centerPointInside.x) * 2 / OutCircleSize;
		float y=(mTouchDownPoint.y - centerPointInside.y) * 2 / OutCircleSize;
		mMoveContral.set(x, y);
		return mMoveContral;
	}
	public Bitmap getbitmap()
	{
		//reDraw();
		return mBitmap;
	}
	private void reDraw()
	{
		mBitmap.eraseColor(Color.argb(0, 0, 0, 0));
		mCanvas.drawCircle(centerPointInside.x, centerPointInside.y, OutCircleSize / 2, mPaint);
		mCanvas.drawCircle(mTouchDownPoint.x, mTouchDownPoint.y, OutCircleSize / 4, mPaint);
		mCanvas.drawText("mTouchDownPoint:" + mTouchDownPoint.x + "," + mTouchDownPoint.y, 50, 50, mPaint);
		mCanvas.drawText("centerInside:" + centerPointInside.x + "," + centerPointInside.y, 50, 100, mPaint);
		mCanvas.drawText("mt-cx=" + (mTouchDownPoint.x - centerPointInside.x) / OutCircleSize, 50, 150, mPaint);
		mCanvas.drawText("mm=" + mMoveContral.x + "," + mMoveContral.y, 50, 200, mPaint);
		mCanvas.drawText("I am new!", 50, 250, mPaint);

	}
	private void setLeftButtonDown(float xInside, float yInside)
	{
		if (Math.pow((xInside - centerPointInside.x), 2)
				+ Math.pow((yInside - centerPointInside.y), 2)
				<= Math.pow((float)(OutCircleSize / 2), 2))
		{
			mTouchDownPoint.set(xInside, yInside);

		}
		else
		{
			if (xInside == centerPointInside.x)
			{
				if (yInside >  centerPointInside.y)
				{
					mTouchDownPoint.set(centerPointInside.x , OutCircleSize / 2 + mSPM.getAttackButtonSize() / 2);
				}
				else
				{
					mTouchDownPoint.set(mSPM.getAttackButtonSize() / 2, 0);
				}
			}
			else if (yInside == OutCircleSize)
			{
				if (xInside >  (int)((float)(OutCircleSize * 3 / 2)))
				{
					mTouchDownPoint.set(OutCircleSize / 2 + mSPM.getAttackButtonSize() / 2, mSPM.getAttackButtonSize() / 2);
				}
				else
				{
					mTouchDownPoint.set(0, mSPM.getAttackButtonSize() / 2);
				}
			}
			else
			{
				double ddx=Math.sqrt(Math.pow(OutCircleSize / 2, 2) / (1 + Math.pow((yInside -  (int)((float)(mSPM.getAttackButtonSize() / 2))) / (xInside -  (int)((float)(mSPM.getAttackButtonSize() / 2))), 2)));
				double x;
				if (xInside > (int)((float)(mSPM.getAttackButtonSize() / 2)))
				{
					x =  (int)((float)(mSPM.getAttackButtonSize() / 2)) + ddx;
				}
				else
				{
					x =  (int)((float)(mSPM.getAttackButtonSize() / 2)) - ddx;
				}
				double y= (int)((float)(mSPM.getAttackButtonSize() / 2)) + (yInside - mSPM.getAttackButtonSize() / 2) * (x - mSPM.getAttackButtonSize() / 2) / (xInside - mSPM.getAttackButtonSize() / 2);
				mTouchDownPoint.set((int)x, (int)y);
			}


		}


	}
	public PointF getLeftTopPoint()
	{
		return LeftTopPoint;
	}
}
